package com.zcx.mall.common.web.handler;

import com.zcx.mall.common.web.api.ApiResult;
import com.zcx.mall.common.web.constants.ErrorCodeConstants;
import com.zcx.mall.common.web.exception.BizExcepton;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
@Slf4j
public class SysExceptionHandler {

    @ResponseBody
    @ExceptionHandler(BizExcepton.class)
    public ApiResult bizException(BizExcepton ex){
        log.error(ex.getMessage(), ex);
        return new ApiResult(ex);
    }

    @ResponseBody
    @ExceptionHandler(RuntimeException.class)
    public ApiResult runtimeException(RuntimeException ex){
        log.error(ex.getMessage(), ex);
        return new ApiResult(ErrorCodeConstants.SystemError.getErrorCode(), ErrorCodeConstants.SystemError.getMessage());
    }
}
